<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="90%" destroy-on-close @closed="$emit('closed')" :align-center="true" :close-on-click-modal="false">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="110px" label-position="top" style="height:450px;overflow:auto">
            <el-form-item label="物料信息" prop="material_data">
                <sc-form-table ref="table" v-model="form.material_data" :addTemplate="addTemplate" placeholder="暂无数据" hideAdd hideDelete>
                    <el-table-column prop="material_name" label="物料名称" align="center"></el-table-column>
                    <el-table-column prop="referred_as" label="供应商" align="center"></el-table-column>
                    <el-table-column prop="color_number" label="物料色号" align="center"></el-table-column>
                    <el-table-column prop="colour" label="物料颜色" align="center"></el-table-column>
                    <el-table-column prop="batch_number" label="缸号" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.batch_number" clearable style="width: 100%" placeholder="请选择" @change="batchChange(scope.row)">
                                <el-option v-for="item in scope.row.batch_stock" :key="item.batch_no" :label="item.batch_no+'【'+item.curr_stock+'KG】'" :value="item.batch_no" />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column prop="store_id" label="仓库" align="center">
                        <template #default="scope">
                            {{scope.row.ware_name}}
                        </template>
                    </el-table-column>

                    <el-table-column prop="weight" label="需发数量" align="center">
                        <template #default="scope">
                            <div>{{scope.row.weight}}</div>
                        </template>
                    </el-table-column>
                    
                    <el-table-column prop="reality_weight" label="实际发毛" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.reality_weight" placeholder="请输入实际发毛"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="remark" label="备注" align="center" width="150">
                        <template #default="scope">
                            <el-input v-model="scope.row.remark" placeholder="请输入备注"></el-input>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">完 成</el-button>
		</template>
	</el-dialog>
</template>
<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '添加客户',
					edit: '发毛',
					over: '完成打板'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
                    contract_no:"",
                    material_data:[],
				},
				//验证规则
				rules: {
                    audit_status: [
						{required: true, message: '请选择'}
					],
				},
                wareList:[],
                groupsProps: {
                    value: "id",
                    emitPath: false,
                    checkStrictly: true
                }
			}
		},
		mounted() {
            // this.getWare()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
                console.log('this.form :>> ', this.form);
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						res = await this.$API.produce.waitList.send.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
                this.form.contract_no = data.contract_no
                var info = data.produceMaterial
                let array = []
                info.map(item=>{
                    array.push({
                        material_name:item.material_name,
                        referred_as:item.materialSpec.material.referred_as,
                        color_number:item.materialSpec.color_number,
                        colour:item.materialSpec.colour,
                        batch_number:"",
                        reality_weight:item.total_no_received,
                        weight:item.total_no_received,

                        total_weight:item.total_no_received,

                        batch_stock:item.batch_stock,
                        material_id:item.material_id,
                        material_spec_code:item.material_spec_code,
                    })
                })
                this.form.material_data = array
			},
            // 获取仓库
            async getWare(){
                var res = await this.$API.all.ware.get();
                this.wareList = res.data;
            },
            batchChange(row){
                row.batch_stock.map(item=>{
                    if(item.batch_no==row.batch_number){
                        row.store_area_id = item.area_id,
                        row.store_location_id = item.location_id,
                        row.store_id = item.warehouse_id,
                        row.ware_name = item.ware_name
                    }
                })
            },
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
